<?php
require_once __DIR__ . '/MedooHead.php';
require_once __DIR__ . '/wxApi.php';
require_once __DIR__ . '/../huiguo/wxConfig.php';

function WxNotify($userid, $TMPid, $data, $page = "pages/index/index")
{
    global $appid, $AppSecret;

    $db = (new MedooSql())->database;
    $wxApi = new WxApi($appid, $AppSecret, 'huiguoTokenData');
    $thisUser = $db->get('huiguo', ['nickName', 'openid'], [
        'id' => $userid
    ]);
    $thisUserNickName = $thisUser['nickName'] ?? '';
    $openid = $thisUser['openid'] ?? null;


    foreach ($data as $key => $value) {
        $data[$key] = str_replace('#userName#', $thisUserNickName, $data[$key]);
    }

    if ($openid) {
        $result = $wxApi->sendSubscribeMsg($openid, $TMPid, $page, $data);
        return $result;
    }


    return null;
}
/*
[
            'thing1' => ['value' => '代理'],
            'thing2' => ['value' => '成为代理成功'],
        ]
'G8AzhyqEx6Nxab3S3tGRlVhRM0KbMzev7Ez5KoDh_6k'

        */
